/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.custom;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ecarx.systemui.plugin.R;

public class CustomSwitch extends FrameLayout {

    private boolean isOpen = false;
    private ImageView ivTrack;
    private ImageView ivThumb;
    private SwitchChangeListener switchChangeListener;
    private float mThumbLength = 0;

    public CustomSwitch(@NonNull Context context) {
        this(context, null);
    }

    public CustomSwitch(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSwitch(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.cust_view_switch, this, true);
        ivTrack = findViewById(R.id.iv_track);
        ivThumb = findViewById(R.id.iv_thumb);
        setOnClickListener(v -> {
            setEnabled(false);
            if (isOpen) {
                isOpen = false;
                ivTrack.setSelected(false);
                doThumbAnimator(false,true);
            } else {
                isOpen = true;
                ivTrack.setSelected(true);
                doThumbAnimator(true,true);
            }
        });
        mThumbLength = getResources().getDimension(R.dimen.dp_100) - getResources().getDimension(R.dimen.dp_34);
    }

    private void doThumbAnimator(boolean isOpen,boolean isUserClick) {
        float fromX, toX;
        if (isOpen) {
            fromX = 0;
            toX = mThumbLength;
        } else {
            fromX = mThumbLength;
            toX = 0;
        }
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ivThumb, "translationX", fromX, toX);
        objectAnimator.setDuration(200);
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                setEnabled(true);
                if (switchChangeListener != null && isUserClick) {
                    if(isOpen){
                        switchChangeListener.toOpen();
                    }else{
                        switchChangeListener.toClose();
                    }
                }
            }
        });
        objectAnimator.start();
    }

    public void uiOpen() {
        if (isOpen) {
            return;
        }
        isOpen = true;
        ivTrack.setSelected(true);
        doThumbAnimator(true,false);
    }

    public void uiClose() {
        if (!isOpen) {
            return;
        }
        isOpen = false;
        ivTrack.setSelected(false);
        doThumbAnimator(false,false);
    }

    public void setSwitchChangeListener(SwitchChangeListener switchChangeListener) {
        this.switchChangeListener = switchChangeListener;
    }

    interface SwitchChangeListener {
        void toClose();

        void toOpen();
    }
}
